﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.AutomaticPathPlanner.Planner.CApAPPKineoForAppTrajectoryCommand
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.Viewers;


namespace DnProcessSimulateCommands.AutomaticPathPlanner.Planner;

public class CApAPPKineoForAppTrajectoryCommand
{
  private bool m_wasPlanningCancelled;
  private bool m_isFinished;
  private bool m_isStarted;
  private bool m_wasPlanningSuccessful;
  private bool m_wasPlanningTimeOut;
  private TxOperationTreeInternal m_operationTree;

  public CApAPPKineoForAppTrajectoryCommand(
    CApAPPTask i_task,
    KiAppPathPlanningParamsEx i_pathPlanningParams)
  {
    this.TaskToPlan = i_task;
    this.PathPlanningParams = i_pathPlanningParams;
    this.m_wasPlanningCancelled = false;
    this.m_wasPlanningTimeOut = false;
    this.m_wasPlanningSuccessful = false;
    this.m_operationTree = new TxOperationTreeInternal();
  }

  public CApAPPTask TaskToPlan { get; }

  public KiAppPathPlanningParamsEx PathPlanningParams { get; }

  public bool IsFinished() => this.m_isFinished;

  public bool WasPlanningSuccessful() => this.m_wasPlanningSuccessful;

  public bool WasPlanningTimeOut() => this.m_wasPlanningTimeOut;

  public double GetProgress() => this.DoGetProgress(this.TaskToPlan);

  protected virtual double DoGetProgress(CApAPPTask i_task)
  {
    return KiAppKineoForAppTrajectoryPlanCommandEx.GetProgress(i_task.KiTask);
  }

  public void Cancel()
  {
    this.m_wasPlanningCancelled = true;
    this.EndPlanning(false);
  }

  public bool WasPlanningCancelled() => this.m_wasPlanningCancelled;

  public CApAPPKineoForAppTrajectoryCommand.EPlanSuccess PlanSuccess()
  {
    CApAPPKineoForAppTrajectoryCommand.EPlanSuccess eplanSuccess = CApAPPKineoForAppTrajectoryCommand.EPlanSuccess.SUCCESS;
    if (this.WasPlanningTimeOut())
      eplanSuccess = CApAPPKineoForAppTrajectoryCommand.EPlanSuccess.TIMEOUT_ERROR;
    else if (this.WasPlanningCancelled())
      eplanSuccess = CApAPPKineoForAppTrajectoryCommand.EPlanSuccess.SKIP;
    else if (!this.WasPlanningSuccessful())
      eplanSuccess = CApAPPKineoForAppTrajectoryCommand.EPlanSuccess.CLASH_ERROR;
    else if (!this.m_isFinished)
      eplanSuccess = this.m_isStarted ? CApAPPKineoForAppTrajectoryCommand.EPlanSuccess.WAIT : CApAPPKineoForAppTrajectoryCommand.EPlanSuccess.WAIT_BEFORE_START;
    return eplanSuccess;
  }

  public void OnIdle()
  {
    if (this.m_wasPlanningCancelled)
      this.EndPlanning(false);
    else
      this.ContinuePlanning();
  }

  public virtual bool StartPlanning()
  {
    this.m_isStarted = true;
    this.m_isFinished = false;
    this.m_operationTree.FreezeTree();
    return this.DoStartPlanning(this.TaskToPlan, this.PathPlanningParams);
  }

  protected virtual bool DoStartPlanning(CApAPPTask i_task, KiAppPathPlanningParamsEx i_params)
  {
    return KiAppKineoForAppTrajectoryPlanCommandEx.StartPlan(i_task.KiTask, i_params);
  }

  private void EndPlanning(bool i_success)
  {
    this.m_operationTree.UnFreezeTree();
    this.DoEndPlanning(this.TaskToPlan, i_success);
    this.m_isStarted = false;
    this.m_isFinished = true;
    this.m_wasPlanningSuccessful = i_success;
  }

  public void UnFreezeOperationTree() => this.m_operationTree.UnFreezeTree();

  protected virtual void DoEndPlanning(CApAPPTask i_task, bool i_success)
  {
    KiAppKineoForAppTrajectoryPlanCommandEx.FinishPlan(i_task.KiTask, i_success);
  }

  private void ContinuePlanning()
  {
    bool o_shouldContinue = false;
    bool i_success = this.DoContinuePlanning(this.TaskToPlan, ref o_shouldContinue);
    if (o_shouldContinue && i_success)
      return;
    this.EndPlanning(i_success);
  }

  protected virtual bool DoContinuePlanning(CApAPPTask i_task, ref bool o_shouldContinue)
  {
    return KiAppKineoForAppTrajectoryPlanCommandEx.ContinuePlan(i_task.KiTask, ref o_shouldContinue);
  }

  public enum EPlanSuccess
  {
    SUCCESS,
    SKIP,
    CLASH_ERROR,
    TIMEOUT_ERROR,
    WAIT,
    WAIT_BEFORE_START,
  }
}
